package com.hfzy.ihk.facade.linkcall.entity.flow.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hfzy.ihk.common.facade.BaseEntity;
import com.hfzy.ihk.common.facade.entity.KeyValueField;
import com.hfzy.ihk.facade.linkcall.enums.SpeakingFlowType;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/19.
 *
 * 话术流程
 * 注：
 *  多伦回话也是一个流程
 * @author wws
 */
@Document(indexName = "#{elasticSearchConfig.indexName()}_flow",shards = 24,replicas = 2, type = "data")
public class SpeakingFlow implements BaseEntity {

    @Id
    @Field(type = FieldType.Keyword)
    private String id;

    private long version;

    /**
     * 空时，表示公共流程，特殊的（比如挂机），没有任何执行步骤
     * */
    @Field(type = FieldType.Keyword)
    private String packageId;

    @Field(type = FieldType.Keyword)
    private String flowName;

/*----------------------暂留，看看是否需要双向链表-------------------------*/
    /**上一级流程Id*/
    @Field(type = FieldType.Keyword)
    private String preNodeId;
    /**下一级流程Id*/
    @Field(type = FieldType.Keyword)
    private String nextNodeId;
    /*----------------------暂留，看看是否需要双向链表-------------------------*/

    private long createUserId;

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss" )
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    	/*fastjson局部更新起作用:* 比如增加一条意向* */
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss" )
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    	/*fastjson局部更新起作用:* 比如增加一条意向* */
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date modTime;

    /**回答分支与下一流程*/
    private List<KeyValueField> flowJson;

    /**流程顺序*/
    @Field(type = FieldType.Integer)
    private int sortNum = 0;

    /**类型: 主动流程、多轮会话流程*/
    @Field(type = FieldType.Keyword)
    private SpeakingFlowType flowType;


    @Override
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public String getFlowName() {
        return flowName;
    }

    public void setFlowName(String flowName) {
        this.flowName = flowName;
    }

    public long getCreateUserId() {
        return createUserId;
    }

    public void setCreateUserId(long createUserId) {
        this.createUserId = createUserId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getPackageId() {
        return packageId;
    }

    public void setPackageId(String packageId) {
        this.packageId = packageId;
    }

    public String getPreNodeId() {
        return preNodeId;
    }

    public void setPreNodeId(String preNodeId) {
        this.preNodeId = preNodeId;
    }

    public String getNextNodeId() {
        return nextNodeId;
    }

    public void setNextNodeId(String nextNodeId) {
        this.nextNodeId = nextNodeId;
    }

    public List<KeyValueField> getFlowJson() {
        return flowJson;
    }

    public void setFlowJson(List<KeyValueField> flowJson) {
        this.flowJson = flowJson;
    }

    @JsonIgnore
    public String getFlowJsonStr(){
        if(this.getFlowJson()!=null && !this.getFlowJson().isEmpty()){
            Map<String,String> keyVals= new HashMap<String,String>();
            for(KeyValueField keyValueField:this.getFlowJson()){
                keyVals.put(keyValueField.getKey(),keyValueField.getValue());
            }
            return  JSON.toJSONString(keyVals);
        }else{
            return "";
        }
    }

    public int getSortNum() {
        return sortNum;
    }

    public void setSortNum(int sortNum) {
        this.sortNum = sortNum;
    }

    public SpeakingFlowType getFlowType() {
        return flowType;
    }

    public void setFlowType(SpeakingFlowType flowType) {
        this.flowType = flowType;
    }

    public Date getModTime() {
        return modTime;
    }

    public void setModTime(Date modTime) {
        this.modTime = modTime;
    }
}
